<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UploadForm;
use backend\models\NavigationIdentification;
use common\helps\tools;
use yii\bootstrap\Nav;
use backend\models\GoodsNavigationIdentificationrelationEdit;

class NavigationIdentificationController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'NavigationIdentification';
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetNavigationIdentification(){
        $data = Yii::$app->request->post();
        $navigation_identification = NavigationIdentification::find()->select('id,parent_id,name,sort,is_show')->orderBy('parent_id asc,sort asc')->asArray()->all();
        $list = [];
        if (is_array($navigation_identification) && count($navigation_identification) > 0){
            foreach ($navigation_identification as $val){
                if ($val['parent_id'] === '0'){
                    $list[$val['id']] = $val;
                }else{
                    $list[$val['parent_id']]['son'][] = $val;
                }
            }
            $list = array_values($list);
        }
        $this->retJson($list, 200);
    }
    
    public function actionChangeNavigationIdentification(){
        $model = new NavigationIdentification();
        $goods_model = new GoodsNavigationIdentificationrelationEdit();
        $data = Yii::$app->request->post();
        $transaction = Yii::$app->db->beginTransaction();
        try {
            $name = $data['name'];
            $sort = $data['sort'];
            $son_id = tools::arrayHasValue($data,'son_id') ? $data['son_id'] : [];
            $is_show = explode(',', $data['is_show']);
            $navigation_identification_id = $data['navigation_identification_id'];
            if (count($name) == 0) {
                throw new \Exception('请填写商品导航标识');
            }
            if (count($name) == 0) {
                throw new \Exception('请填写商品导航标识排序');
            }
            $creator = $model->getCreator();
            $oper_time = date('Y-m-d H:i:s',time());
            if ($navigation_identification_id){
                $model = $model->findOne($navigation_identification_id);
                if ($is_show[0] == '0'){
                    $goods_nav_count = $goods_model->find()->where(['navigation_identification_id'=>$navigation_identification_id])->count();
                    if (intval($goods_nav_count) > 0){
                        throw new \Exception('一级商品导航标识不可不展示，此导航标识已被商品使用');
                    }
                }
                $model->editor = $creator;
                $model->edit_time = $oper_time;
            }else{
                $model->creator = $creator;
                $model->create_time = $oper_time;
            }
            $model->name = $name[0];
            $model->sort = intval($sort[0]);
            $model->is_show = $is_show[0];
            $model->save();
            $navigation_identification_id = $model->attributes['id'];
            if (!$navigation_identification_id){
                throw new \Exception('操作失败，请重试');
            }
            $find_nav = NavigationIdentification::find()->where(['parent_id'=>$navigation_identification_id])->asArray()->all();
            $old_id = is_array($find_nav) && count($find_nav) > 0 ? array_column($find_nav, 'id') : '';
            $find_nav = array_combine(array_column($find_nav, 'id'), $find_nav);
            if (count($son_id) > 0){
                for ($i=0;$i<count($son_id);$i++){
                    $model = new NavigationIdentification();
                    $num = $i+1;
                    if ($son_id[$i] == '0'){
                        $model->creator = $creator;
                        $model->create_time = $oper_time;
                    }else{
                        $model = $model->findOne($son_id[$i]);
                        if ($is_show[$num] == '0'){
                            $goods_nav_count = $goods_model->find()->where(['navigation_identification_id'=>$son_id[$i]])->count();
                            if (intval($goods_nav_count) > 0){
                                throw new \Exception('二级商品导航标识【'.$name[$num].'】不可不展示，此导航标识已被商品使用');
                            }
                        }
                        $model->editor = $creator;
                        $model->edit_time = $oper_time;
                    }
                    $model->parent_id = $navigation_identification_id;
                    $model->name = $name[$num];
                    $model->sort = $sort[$num];
                    $model->is_show = $is_show[$num];
                    $model->save();
                    $son_navigation_identification_id = $model->attributes['id'];
                    if (!$son_navigation_identification_id){
                        throw new \Exception('操作失败，二级标识添加失败');
                    }
                }
            }
            $ids = [];
            if (is_array($old_id) && count($old_id) > 0){
                foreach ($old_id as $id){
                    if (!in_array($id, $son_id)){
                        $goods_nav_count = $goods_model->find()->where(['navigation_identification_id'=>$id])->count();
                        if (intval($goods_nav_count) > 0){
                            throw new \Exception('二级商品导航标识【'.$find_nav[$id]['name'].'】不可删除，此导航标识已被商品使用');
                        }
                        $ids[] = $id;
                    }
                }
                if (count($ids) > 0){
                    $del = NavigationIdentification::deleteAll(['id'=>$ids]);
                    if(!$del){
                        throw new \Exception('操作失败，二级标识删除失败');
                    }
                }
            }
            $transaction->commit();
            $this->retJson('编辑成功',200);
        }catch (\Exception $e){
            $transaction->rollBack();
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            $this->retJson($e->getMessage(),500);
        }
    }
    
    public function actionGetNavigationIdentificationDetail(){
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $where = [
            'or',
            ['id'=>$id],
            ['parent_id'=>$id],
        ];
        $navigation_identification = NavigationIdentification::find()->select('id,parent_id,name,sort,is_show')->where($where)->orderBy('parent_id asc')->asArray()->all();
        $list = [];
        if (is_array($navigation_identification) && count($navigation_identification) > 0){
            foreach ($navigation_identification as $val){
                if ($val['parent_id'] === '0'){
                    $list = $val;
                }else{
                    $list['son'][] = $val;
                }
            }
        }
        $this->retJson($list, 200);
    }
}